#include <Arduino.h>
#include "src/display.h"
#include "src/pwifi.h"
#include "src/audio.h"
#include "src/application.h"
#include "src/mtime.h"
#include "src/aht20.h"
#include <Ticker.h>
#include <OneButton.h>

#define BLINKER_PIN 4
#define ANGLE_BUTTON_PIN 13
#define TOUCH_BUTTON_PIN 2
// angle button
OneButton angle_btn(ANGLE_BUTTON_PIN, true);
OneButton touch_btn(TOUCH_BUTTON_PIN, false);

Ticker my_ticker_mode_switch;
ModeType modes[] = {
    MODE_TIME,
    MODE_DATE,
    MODE_ANIM_RAINING,
    MODE_AUDIO_SPECTURM,
    MODE_WEATHER, MODE_AHT20};
int mode_idx = 0;
ModeType currentMode = modes[mode_idx];

void setup()
{
  Serial.begin(115200);
  delay(200);
  pinMode(BLINKER_PIN, OUTPUT);

  displayManager = new MatrixDisplayer();

  displayManager->ShowMessage("PMATRIX", 7, displayManager->Color(255, 0, 255));
  delay(1000);

  wifi_init_v2();
  sync_sys_time();
  // audio_init();
  audio_inmp441_init();
  audio_max98357_mp3_init();
  aht20_init();
  angle_btn_init();

  mode_init();
  my_ticker_mode_switch.attach(70, mode_switch_cb);

  Serial.print("=======================================\r\nPMatrix启动!\r\n");
}

void loop()
{
  // http_test();
  // displayManager->Example_Neo_Print();
  // displayManager->Example_Neo_Image();

  // audio_test_mic_to_speaker();
  // audio_inmp441_test();
  switch_mode(currentMode);
  audio_max98357_mp3_loop();

  angle_btn.tick();
  touch_btn.tick();

  delay(TICK_SYS);
}

void led_blink()
{
  digitalWrite(BLINKER_PIN, !digitalRead(BLINKER_PIN));
}

void angle_btn_init()
{
  pinMode(ANGLE_BUTTON_PIN, INPUT);
  pinMode(TOUCH_BUTTON_PIN, INPUT);

  angle_btn.reset();
  angle_btn.setPressTicks(500); // ms 按住按钮以触发长按的持续时间。
  angle_btn.attachLongPressStart(angle_btn_long_click);
  angle_btn.attachLongPressStop(angle_btn_long_click_stop);

  touch_btn.reset();
  touch_btn.setPressTicks(500);
  touch_btn.attachClick(touch_btn_click);
  touch_btn.attachDoubleClick(touch_btn_double_click);
  touch_btn.attachMultiClick(touch_btn_multi_click);
  touch_btn.attachLongPressStop(touch_btn_long_click_over);
  touch_btn.attachDuringLongPress(touch_btn_during_long_click);
}

void angle_btn_long_click()
{
  Serial.println("angle angle_btn long click");
  currentMode = MODE_TIMER_5MIN;
}

void angle_btn_long_click_stop()
{
  Serial.println("angle angle_btn long click stop");
  currentMode = modes[mode_idx];
}

void touch_btn_click()
{
  Serial.println("touch_btn_click");

  if (currentMode == MODE_TIMER_5MIN)
    return;

  mode_idx++;
  if (mode_idx >= sizeof(modes) / sizeof(modes[0]))
    mode_idx = 0;
  currentMode = modes[mode_idx];
}

void touch_btn_double_click()
{
  Serial.println("touch_btn_double_click");
}

void touch_btn_multi_click()
{
  Serial.println("touch_btn_multi_click");
  int num = touch_btn.getNumberClicks();
  if (num == 3)
  {
    // audio_max98357_mp3_start();
    audio_max98357_mp3_play(MP3_URL_TEST, true);
  }
  else if (num == 5)
  {
    wifiManager.resetSettings();
  }
}

static uint8_t brightness_now = MATRIX_BRIGHTNESS;
static bool is_up = true;
const uint8_t brightness_max = 200;
const uint8_t brightness_min = 10;
const uint8_t step = 1;
void touch_btn_long_click_over()
{
  Serial.println("touch_btn long click over");
  is_up = !is_up;
}

void touch_btn_during_long_click()
{
  Serial.println("touch_btn during long click");

  if (is_up)
    brightness_now += step;
  else
    brightness_now -= step;

  if (brightness_now >= brightness_max)
    brightness_now = brightness_max;
  else if (brightness_now <= brightness_min)
    brightness_now = brightness_min;

  displayManager->setBrightness(brightness_now);
}

void mode_switch_cb()
{
  // 假装按键触发切换
  touch_btn_click();
}